package com.aispeech.detector.receiver;

import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

/**
 * 监听休眠状态
 * 当前根据屏幕是否关闭来做简单判断
 */
public class PowerStateReceiver extends BroadcastReceiver {
    private static final String TAG = "PowerStateReceiver";

    //唤醒屏幕并解锁
    public void wakeupScreen(Context context) {
        Log.d(TAG, "wakeUpAndUnlock: ------------ ");
//        KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//        KeyguardManager.KeyguardLock kl = km.newKeyguardLock("unLock");
//        //解锁
//        kl.disableKeyguard();
        //获取电源管理器对象
        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        //获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
//        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass().getName());
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.SCREEN_DIM_WAKE_LOCK, this.getClass().getName());
        //点亮屏幕
        wl.acquire();
        //释放
        wl.release();
    }

    @Override
    public void onReceive(final Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(TAG, "action: " + action);

        if (Intent.ACTION_SCREEN_OFF.equals(action)) {
            Log.e(TAG, "screen off -------------- ");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(3000);
                        wakeupScreen(context);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        } else if (Intent.ACTION_SCREEN_ON.equals(action)) {
            Log.e(TAG, "screen on --------------- ");

        }
    }
}
